package com.cg.controller;

import com.cg.client.UserClient;
import com.cg.dto.Dto;
import com.cg.dto.OrderManagerDto;
import com.cg.vo.LoginUserVo;
import com.cg.vo.OrderManagerVo;
import com.cg.vo.QueryOrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/v")
@Slf4j
public class OrderController {

    @Resource
    private UserClient userClient;

    //根据订单状态、订单创建时间、关键词(商品名称，订单号，商品编号模糊)
    @PostMapping("/queryOrderList")
    public Dto<List<OrderManagerVo>> queryOrderList(HttpServletRequest request, @RequestBody OrderManagerDto orderManagerDto){
        String token = request.getHeader("token");
        Dto<LoginUserVo> loginUserVoDto = userClient.queryUserInfoByToken(token);
        orderManagerDto.setUserId(loginUserVoDto.getData().getUserId());
        return userClient.queryOrderList(orderManagerDto);
    }


    //根据订单号查询订单
    @PostMapping("/queryOrderByOrderNo")
    public Dto<QueryOrderVo> queryOrderByOrderNo(@RequestBody Map<String,String> map){
        String orderNo = map.get("orderNo");
        return userClient.queryOrderByOrderNo(orderNo);
    }


    //订单支付
    @GetMapping("/apliPay/toPay/{orderNo}")
    public String toPay(@PathVariable("orderNo") String orderNo) throws Exception{
        return userClient.toPay(orderNo);
    }


    //接收同步通知
    @GetMapping("/aliReturn")
    public String aliReturn(HttpServletRequest request){
        log.info("同步通知:{}接收到同步通知");
        Map<String, String[]> parameterMap = request.getParameterMap();
        return userClient.returnUrl(parameterMap);
    }

    //接收异步通知
    @PostMapping("/aliNotify")
    public String aliNotify(HttpServletRequest request){
        log.info("异步通知:{}接收到异步通知");
        Map<String, String[]> parameterMap = request.getParameterMap();
        return userClient.notifyUrl(parameterMap);
    }
}
